﻿<%@ WebHandler Language="C#" Class="Turnover" %>

using System;
using System.Web;
using Chenxin.Lib.Utility;

public class Turnover : AjaxHandlerBase
{
    public Turnover()
    {
        //导出
        htTypePermit.Add("export", Constants.Permit_Turnover_Export);
    }
    override public void RequestProcess(HttpContext context)
    {
        try
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";

            string strOp = context.Request["op"];
            string strRet = string.Empty;
            switch (strOp.ToLower())
            {
                case "getlist":
                    GetList(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Chenxin.Lib.Utility.Constants.ExecResult.Error, ex.Message, ""));
        }
    }


    private void GetList(HttpContext context)
    {
        //查询条件
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        //权限
        if (CurrentUserInfo.ContainsPermit(Constants.Permit_Turnover_All))
        {
            sb.Append(" (1=1) ");
        }
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_Turnover_City))
        {
            sb.AppendFormat(" (BuyerDepartId in (Select DepartId from v_Hr_DepartmentInfo WHERE cityid='{0}')) ", CurrentUserInfo.CityId.ToString());
        }
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_Turnover_Company))
        {
            sb.AppendFormat(" (BuyerDepartId in (select DepartId from v_Hr_DepartmentInfo WHERE companyid='{0}')) ", CurrentUserInfo.CompanyId.ToString());
        }
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_Turnover_Depart))
        {
            sb.Append(" (EXISTS(");
            sb.AppendFormat("Select 1 from t_Business_EmpSetting M WHERE t_Contract_BaseInfo.BuyerDepartId=M.DepartId And M.Userid= '{0}' and SearchTurnover=1", CurrentUserInfo.UserId.ToString());
            sb.Append(")) ");
        }
        else
            sb.Append(" (1=0) ");
        
        //4级
        Guid city = (string.IsNullOrEmpty(context.Request["cityid"])) ? Guid.Empty : new Guid(context.Request["cityid"]);
        Guid company = (string.IsNullOrEmpty(context.Request["companyid"])) ? Guid.Empty : new Guid(context.Request["companyid"]);
        Guid businessdistrict = (string.IsNullOrEmpty(context.Request["areaid"])) ? Guid.Empty : new Guid(context.Request["areaid"]);
        Guid departs = (string.IsNullOrEmpty(context.Request["departid"])) ? Guid.Empty : new Guid(context.Request["departid"]);

        if (departs != Guid.Empty)
        {
            sb.AppendFormat(" and BuyerDepartId = '{0}'", departs);
        }
        else if (businessdistrict != Guid.Empty)
        {
            Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo item = null;
            string strCachkey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPARTMENT, businessdistrict);
            Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
            item = cs.GetObject<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>(strCachkey);
            if (!cs.Hit)
            {
                item = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(businessdistrict);
                if (item != null) cs.Set(strCachkey, item, DateTime.Now.AddDays(1));
            }

            if (item == null)
            {
                context.Response.Write(retExecResult(Chenxin.Lib.Utility.Constants.ExecResult.Error, "获取商圈或分类数据丢失", ""));
            }
            sb.AppendFormat(" and (BuyerDepartId in (Select DepartId from v_Hr_DepartmentInfo where DepartSrc like '{0}%'))", item.DepartSrc);
        }
        else if (company != Guid.Empty)
        {
            sb.AppendFormat(" and (BuyerDepartId in (select DepartId from v_Hr_DepartmentInfo WHERE companyid='{0}')) ", company);
        }
        else if (city != Guid.Empty)
        {
            sb.AppendFormat(" and (BuyerDepartId in (Select DepartId from v_Hr_DepartmentInfo WHERE cityid='{0}')) ", city);
        }
        
        //时间
        if (!string.IsNullOrEmpty(context.Request["StartTime"]))
            sb.AppendFormat(" and ArchivementDate>='{0}' ", context.Request["StartTime"]);
        if (!string.IsNullOrEmpty(context.Request["EndTime"]))
            sb.AppendFormat(" and ArchivementDate<='{0}' ", context.Request["EndTime"]);
        
        

        sb.Append(" group by BuyerDepartId ");

        //排序
        if (!string.IsNullOrEmpty(context.Request["sort"]))
            sb.AppendFormat(" order by {0} ", context.Request["sort"]);
        else
            sb.Append(" order by  BuyerDepartId");
        
        
        string strSql = "select BuyerDepartId,dbo.FN_GetBussinessByDepartID(BuyerDepartId) as business,dbo.FN_GetDepartNameByDepartID(BuyerDepartId) as departname,"
                        +"sum(case when BusinessType=10  then 1 else 0 end ) as m_count,"
                        +"sum(case when BusinessType=10  then Area else 0 end ) as m_area,"
                        +"sum(case when BusinessType=10  then BargainPrice else 0 end ) as m_price,"
                        +"sum(case when BusinessType=20  then 1 else 0 end ) as z_count,"
                        +"sum(case when BusinessType=20  then Area else 0 end ) as z_area,"
                        +"sum(case when BusinessType=20  then BargainPrice else 0 end ) as z_price,"
                        + "sum(case when BusinessType=30  then 1 else 0 end ) as l_count,"
                        + "sum(case when BusinessType=30  then Area else 0 end ) as l_area,"
                        + "sum(case when BusinessType=30  then BargainPrice else 0 end ) as l_price,"
                        + "sum(case when BusinessType=40  then 1 else 0 end ) as q_count, "
                        + "sum(case when BusinessType=40  then Area else 0 end ) as q_area,"
                        + "sum(case when BusinessType=40  then BargainPrice else 0 end ) as q_price,"
                        + "sum(case when BusinessType=50  then 1 else 0 end ) as ld_count, "
                        + "sum(case when BusinessType=50  then Area else 0 end ) as ld_area,"
                        + "sum(case when BusinessType=50  then BargainPrice else 0 end ) as ld_price "
                        + "from dbo.v_Contract_Linkage where " + sb.ToString();

        System.Data.DataSet ds = Chenxin.Lib.Business.DAL.v_Contract_CommisionDetailInfo.GetList(strSql);

        context.Response.Write(string.Format("{{success:1,msg:'',data:{{count:{0},{1}}}}}", ds.Tables[0].Rows.Count, Chenxin.Lib.Utility.JsonHelp.Convert2Json(ds.Tables[0])));

    }
}